package red.lixiang.controller;

import red.lixiang.model.BaseInfo;
import red.lixiang.service.InventoryService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.logging.Logger;

@Controller
public class InventoryController {
    private final Logger logger = Logger.getLogger(InventoryController.class.getName());


    @Autowired
    private InventoryService inventoryService;


    @GetMapping("/inventory/showInventoryPage")
    public String showInventoryPage() {
        logger.info("come in inventory");
        return "inventory";
    }

    @GetMapping("/inventory/getAllProduct")
    @ResponseBody
    public String getAllProduct() {
        BaseInfo productInfo = inventoryService.getAllProduct();
        return JSON.toJSONString(productInfo.getObject());
    }

//    @PostMapping("/inventory/importProduct")
//    @ResponseBody
//    public String importProduct(MultipartHttpServletRequest multiReq) {
//        String uploadFilePath = multiReq.getFile("file_data").getOriginalFilename();
//        List<Product> products = null;
//        try {
//            InputStream fileIs = multiReq.getFile("file_data").getInputStream();
//
//
//            products = IS_ExcelUtils.excelToListForXLS(fileIs, Product.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        BaseInfo baseInfo = inventoryService.insertProduct(products);
//
//        System.out.println(uploadFilePath);
//
//        return JSON.toJSONString(products);
//    }
//
//    @PostMapping("/inventory/uploadImage")
//    @ResponseBody
//    public String uploadImage(MultipartHttpServletRequest multiReq) {
//        String uploadFileName = multiReq.getFile("file_data").getOriginalFilename();
//        List<Product> products = null;
//        try {
//            inventoryService.updateProductImage(uploadFileName);
//            byte[] fileIs = multiReq.getFile("file_data").getBytes();
//            String outputPath= "/home/files/demeng/"+uploadFileName;
//            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputPath));
//            bufferedOutputStream.write(fileIs);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//        return JSON.toJSONString(products);
//    }
//
//    @PostMapping("/inventory/updateProduct")
//    @ResponseBody
//    public String updateProduct(@RequestBody  Product product){
//
//        BaseInfo baseInfo= inventoryService.updateProduct(product);
//
//        return JSON.toJSONString(baseInfo);
//    }


}
